package net.itgoo.parkingpay.ui.signin;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import net.itgoo.parkingpay.R;
import net.itgoo.parkingpay.manage.ParkingManage;
import net.itgoo.parkingpay.ui.main.ParkingMainActivity;
import net.itgoo.parkingpay.vendor.mvp.ParkingMVPUtils;
import net.itgoo.parkingpay.vendor.widget.activity.ParkingBaseActivity;

public class ParkingSigninActivity extends ParkingBaseActivity {

    private boolean mInitUI = false;
    private boolean mIsFirst = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parking_activity_signin);
        if (!autoSignIn()) {
            mIsFirst = false;
            initUI();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!mInitUI && !mIsFirst) {
            initUI();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mIsFirst = false;
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    private void initUI() {
        showMainFragment();
    }

    private void showMainFragment() {
        ParkingSigninFragment fragment =
                (ParkingSigninFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.parking_activity_signin_content_fl);

        if (fragment == null) {
            fragment = ParkingSigninFragment.newInstance(getIntent().getExtras());
            ParkingMVPUtils.addFragmentToActivity(getSupportFragmentManager(),
                    fragment, R.id.parking_activity_signin_content_fl);
        }

        new ParkingSigninPresenter(null, fragment, new ParkingSigninDataRepository());

        mInitUI = true;
    }

    private boolean autoSignIn() {
        if (!ParkingManage.getInstance(getApplicationContext()).getUserId().isEmpty()) {
            Intent intent = new Intent(this, ParkingMainActivity.class);
            startActivity(intent);
            return true;
        }
        return false;
    }
}
